package com.xigua.xcar.shoplist;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xigua.xcar.R;
import com.xigua.xcar.base.BaseActivity;
import com.xigua.xcar.constant.Constants;
import com.xigua.xcar.constant.LocationPoint;
import com.xigua.xcar.data.ShopListData;
import com.xigua.xcar.net.AbstractDataManager;
import com.xigua.xcar.net.getnearbydata.GetNearbyData;
import com.xigua.xcar.net.getnearbydata.GetNearbyDataManager;
import com.xigua.xcar.shopdetail.ShopDetailActivity;
import com.xigua.xcar.util.CommonListUtil;
import com.xigua.xcar.widgets.ShopList.ShopListAdapter;

public class ShopListActivity extends BaseActivity {
	private static final String tag = "ShopListActivity";
	private ShopListAdapter mAdapter = null;
	private ArrayList<ShopListData> mArray = null;
	private ListView mListView = null;
	private ProgressDialog mProgressDialog = null; // 加载对话框
	private GetNearbyDataManager mDM = null;
	private boolean isGettingData = false;
	private ProgressBar mLoadingBar = null;
	private TextView mTxtMore = null;
	private OnItemClickListener mListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int arg2,
				long arg3) {
			ShopListData data = (ShopListData) view.getTag();
			Intent intent = new Intent(ShopListActivity.this,
					ShopDetailActivity.class);
			intent.putExtra(Constants.SHOPID, data.getShopNum());
			ShopListActivity.this.startActivity(intent);
		}
	};
	private int mPageIndex = 0;
	@Override
	protected void finalize() throws Throwable {
		// 注销的注意事项
		super.finalize();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i(tag, "onCreate");
		super.onCreate(savedInstanceState);
		setView(R.layout.shoplist);
		setTopBarTitle("店铺列表");
		setTitleBar(R.drawable.title_button, "返回", 0, "");
		Intent intent = getIntent();
		int mRequestType = 0;
		int mIsNearby = 0;
		String mRequestKeyWord = null;
		mRequestType = intent.getIntExtra(Constants.REQUEST_TYPE, 1);
		mRequestKeyWord = intent.getStringExtra(Constants.REQUEST_KEYWORD);
		mIsNearby = intent.getIntExtra(Constants.REQUEST_ISNEARBY, 0);
		if (null == mRequestKeyWord) {
			Log.i(tag, "mRequestType:" + mRequestType);
		} else {
			Log.i(tag, "mRequestType:" + mRequestType + ",mRequestKeyWord:"
					+ mRequestKeyWord);
		}
		mDM = new GetNearbyDataManager(this);
		mDM.setHanlder(new Handler(){

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				if (mProgressDialog != null) {
					mProgressDialog.dismiss();
					mProgressDialog = null;
				}
				isGettingData = false;
				mLoadingBar.setVisibility(View.GONE);
				mTxtMore.setVisibility(View.VISIBLE);
				switch(msg.what){
				case AbstractDataManager.RESULTCODE_SUCCESS:{
					GetNearbyData data = (GetNearbyData)msg.obj;
					if(null != data){
						mPageIndex++;
						ArrayList<ShopListData> datas = data.getArray();
						for(int i=0;i<datas.size();i++){
							mArray.add(datas.get(i));
						}
						mAdapter.notifyDataSetChanged();
					}else{
						if(0 == mArray.size()){
							Toast.makeText(ShopListActivity.this, "暂未找到你搜索的结果", 3000).show();
						}else{
							Toast.makeText(ShopListActivity.this, "亲，到底了哦", 3000).show();
						}
					}
					break;
				}
				case AbstractDataManager.RESULTCODE_NETERR:
				case AbstractDataManager.RESULTCODE_OTHERERR:{
					Toast.makeText(ShopListActivity.this, "亲，风筝断线了", 3000).show();
					break;
				}
				}
			}
			
		});
		LocationPoint location = CommonListUtil.getLocationPointFromSP(this);
		mDM.setLocation(location.getLontitude(),location.getLatitude());
		mDM.setSearchKey(mRequestKeyWord);
		mDM.setSearchType(mRequestType);
		mDM.setIsNearby(mIsNearby);
		mArray = new ArrayList<ShopListData>();
		initData();
		mListView = (ListView) findViewById(R.id.listview_shoplist_main);
		mAdapter = new ShopListAdapter(this);
		mAdapter.setArray(mArray);
		mListView.setOnItemClickListener(mListener);
		LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		RelativeLayout layout = (RelativeLayout)inflater.inflate(R.layout.shoplist_more, null);
		mListView.addFooterView(layout);
		mListView.setAdapter(mAdapter);
//		RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout_shoplist_more);
		mLoadingBar = (ProgressBar)layout.findViewById(R.id.progress_morelayout_progress);
		mTxtMore = (TextView)layout.findViewById(R.id.txt_morelayout_more);
		layout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(isGettingData){
					return ;
				}
				mLoadingBar.setVisibility(View.VISIBLE);
				mTxtMore.setVisibility(View.GONE);
				sendRequest();
			}
		});
	}

	/**
	 * 显示加载进度条
	 * */
	private void showProgressDlg() {
		// HomePageActivityGroup parent = (HomePageActivityGroup)getParent();
		mLoadingBar.setVisibility(View.VISIBLE);
		mTxtMore.setVisibility(View.GONE);
	}

	private void sendRequest() {
		if(null != mDM){
			if(!isGettingData){
				isGettingData = true;
				mDM.setPageIndex(mPageIndex);
				mDM.getDataFromServer();
			}
		}
	}

	private void cancelRequest() {
		isGettingData = false;
		mDM.cancelRequest();
	}

	private void initData() {
//		ShopListData data = new ShopListData();
//		data.setImageUrl("");
//		data.setShopName("滨江小拇指");
//		data.setSaleInfo("洗车：15元/次");
//		data.setDistance("150米");
//		data.setShopNum(1000);
//		mArray.add(data);
	}

	@Override
	protected void handleTitleBarEvent(int buttonId) {
		switch (buttonId) {
		case Constants.LEFTBUTTONID: {
			this.finish();
			break;
		}
		case Constants.RIGHTBUTTONID: {
			break;
		}
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		cancelRequest();
		mLoadingBar.setVisibility(View.GONE);
		mTxtMore.setVisibility(View.VISIBLE);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if(0 == mArray.size()){
			showProgressDlg();
			sendRequest();
		}
	}
	
	
}
